package com.xhj.bigfactory.bigFactory.ThreadPool;

import java.util.Random;
import java.util.concurrent.*;

/**
 * -
 *
 * @author 周沫 @Date 2021-07-31 @Time 20:13
 */
public class MyThreadPoolDemo {
  public static void main(String[] args) {
    System.out.println("Thread.activeCount() = " + Thread.activeCount());
    ThreadPoolExecutor my =
        new ThreadPoolExecutor(
            2,
            5,
            1000,
            TimeUnit.MILLISECONDS,
            new LinkedBlockingDeque<>(3),
            Executors.defaultThreadFactory(),
            //		    new ThreadPoolExecutor.AbortPolicy());
            //		    new ThreadPoolExecutor.CallerRunsPolicy());
            //		    new ThreadPoolExecutor.DiscardOldestPolicy());
            new ThreadPoolExecutor.DiscardPolicy());
    try {
      for (int i = 0; i < 20; i++) {
        int finalI = i;
        my.execute(
            () -> {
              int time = new Random().nextInt(500) + 500;
              try {
                Thread.sleep(time);
              } catch (Exception e) {
                e.printStackTrace();
              } finally {
              }
              System.out.println(
                  Thread.currentThread().getName() + ": " + (finalI + 1) + ", sleep " + time);
            });
      }
    } catch (Exception e) {
      System.out.println(e);
    } finally {
    }

    new Thread(
            () -> {
              System.out.println(Thread.currentThread().getName() + ": come in...");
              try {
                Thread.sleep(500);
              } catch (Exception e) {
                e.printStackTrace();
              } finally {
              }
              System.out.println(
                  "                            Thread.activeCount() = " + Thread.activeCount());
              try {
                Thread.sleep(500);
              } catch (Exception e) {
                e.printStackTrace();
              } finally {
              }
              System.out.println(
                  "                            Thread.activeCount() = " + Thread.activeCount());
              try {
                Thread.sleep(500);
              } catch (Exception e) {
                e.printStackTrace();
              } finally {
              }
              System.out.println(
                  "                            Thread.activeCount() = " + Thread.activeCount());
              try {
                Thread.sleep(500);
              } catch (Exception e) {
                e.printStackTrace();
              } finally {
              }
              System.out.println(
                  "                            Thread.activeCount() = " + Thread.activeCount());
              try {
                Thread.sleep(500);
              } catch (Exception e) {
                e.printStackTrace();
              } finally {
              }
              System.out.println(
                  "                            Thread.activeCount() = " + Thread.activeCount());
            },
            "t")
        .start();
  }
}
